﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RVEAd.Business.Model
{
    public partial class RVEAdsEntities
    {
        #region Client

        /// <summary>
        /// Get all clients
        /// </summary>
        /// <returns></returns>
        public List<Client> GetAllClients()
        {
            return this.Clients.Include("ClientCategory")
                .OrderBy(c => c.Nume)
                .ToList();
        }

        /// <summary>
        /// Add client
        /// </summary>
        /// <param name="clientName"></param>
        public void AddClient(string clientName, int idCategory)
        {
            Client c = new Client();
            c.Nume = clientName;
            c.ClientCategory = this.ClientCategories.Where(i => i.ID == idCategory).FirstOrDefault();

            this.AddToClients(c);
        }

        /// <summary>
        /// Update client
        /// </summary>
        /// <param name="id"></param>
        /// <param name="c"></param>
        public void UpdateClient(Client c, int idCategory)
        {
            var client = this.Clients.Where(item => item.ID == c.ID).FirstOrDefault();

            client.ClientCategory = this.ClientCategories.Where(i => i.ID == idCategory).FirstOrDefault();
            client.Nume = c.Nume;
        }

        /// <summary>
        /// Detele client
        /// </summary>
        /// <param name="id"></param>
        public void DeleteClient(int id)
        {
            var client = this.Clients.Where(item => item.ID == id).FirstOrDefault();

            this.DeleteObject(client);
        }
        
        #endregion

      



        #region ClientAd - Generation Plan

        /// <summary>
        /// Get all clients ads by month and year of the date
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public List<ClientAd> GetClientAds(int clientId, DateTime date)
        {
            return
                (from ad in this.ClientAds.Include("Client")
                 where
                    ad.CreateDate.Year == date.Year &&
                    ad.CreateDate.Month == date.Month &&
                    ad.Client.ID == clientId
                 select ad).ToList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="date"></param>
        /// <param name="numberOfBroadcasts"></param>
        public void CreateClientAd(int clientId, DateTime date, string spotFilename, int numberOfBroadcasts)
        {
            // search for existing CliendAds plans
            // clientID, spot filename, year and month
            var queryCliendAd = from clientAd in this.ClientAds.Include("Client").Include("AdPlans")
                                where
                                    clientAd.Client.ID == clientId &&
                                    clientAd.FileName == spotFilename &&
                                    clientAd.CreateDate.Year == date.Year &&
                                    clientAd.CreateDate.Month == date.Month
                                select clientAd;


            // delete AdPlan and Client
            if (queryCliendAd.Count() > 0)
            {
                foreach (ClientAd clientAd in queryCliendAd)
                {
                    this.DeleteObject(clientAd);
                }
            }

            // generate plan
            this.GenerateAdPlan(clientId, date, spotFilename, numberOfBroadcasts);
        }

        /// <summary>
        /// Generate AdPlan: ClientAd + AdPlan
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="date"></param>
        /// <param name="spotName"></param>
        /// <param name="broadcastNo"></param>
        private void GenerateAdPlan(int clientId, DateTime date, string spotName, int broadcastNo)
        {
            List<DateTime> dates = GetMonthWorkingDays(date);

            ClientAd clientAd = new ClientAd();
            clientAd.Client = this.Clients.Where(i => i.ID == clientId).FirstOrDefault();
            clientAd.CreateDate = date;
            clientAd.FileName = spotName;
            clientAd.BroadcastNo = broadcastNo;
            
            this.AddToClientAds(clientAd);


            foreach (var data in dates)
            {
                AdPlan adPlan = new AdPlan();
                adPlan.ClientAd = clientAd;
                adPlan.PlanDate = data;
                adPlan.PlanDay = data.Day;

                this.AddToAdPlans(adPlan);
            }
        }


        /// <summary>
        /// Working days includes Saturday
        /// </summary>
        /// <returns></returns>
        private static List<DateTime> GetMonthWorkingDays(DateTime date)
        {
            //TODO: New common project

            var currentDate = date;
            int monthDays = DateTime.DaysInMonth(currentDate.Year, currentDate.Month);

            IEnumerable<DateTime> monthDates = Enumerable.Range(1, monthDays)
                .Select(d => new DateTime(currentDate.Year, currentDate.Month, d));

            List<DateTime> list = new List<DateTime>();

            foreach (var day in monthDates)
            {
                if (day.DayOfWeek != DayOfWeek.Sunday)
                {
                    list.Add(day);
                }
            }

            return list;
        }

        #endregion
    }
}
